<?php
namespace app\wap\controller;

use think\Controller;
use app\wap\model\Member;
use app\wap\model\Project;
use app\wap\model\OrderContract;
use app\index\model\Contract;
use think\Db;
use think\Request;
use think\Config;
use expand\BestSignDemo;
use expand\Yunst;

class MemberContract extends Common
{
    const PERPAGE = 10;
    public function _initialize(){
        parent::_initialize();

    }

    /**
     * 个人中心首页
     * @param Request $request
     * @author hey_heyang(635870453@qq.com)
     */
    public function index(Request $request){
        if($request->isPost()){
            $user_info = $request->user_info;
            $param = $request->param();
            $ocModel =new OrderContract();
            $where['oc.user_id'] = $user_info['id'];
            if (empty($param['curpage'])) {
                $curpage = 1;
            } else {
                $curpage = $param['curpage'];
            }
            if(!empty($param['search_key'])){
                $where['oc.title'] = ['like','%'.trim($param['search_key']).'%'];
            }
            $dataList = $ocModel->getMemberContractList($where);
            $total_count = count($dataList);
            $total_page = ceil($total_count/self::PERPAGE);
            $data_list = pageArray($dataList,self::PERPAGE,$curpage);
            outPutData(array('list'=>$data_list,'total_page'=>$total_page,'total_count'=>$total_count));
        }
    }

    /**
     * 查看合同签署
     * @param Request $request
     * @author hey_heyang(635870453@qq.com)
     * @return array
     */
    public function Seecontract(Request $request)
    {
        if($request->isPost()) {
            $Best_data = new BestSignDemo();
            $ocModel = new OrderContract();
            $user_info = $request->user_info;
            $param = $request->param();
            if (empty(intval($param['id']))) {
                outPutData('', '合同参数错误', Config::get('code.tipsCode'));
            }
            $where = ['id' => intval($param['id']), 'user_id' => $user_info['id']];
            $ContInfo = $ocModel->where($where)->find();

            if (empty($ContInfo['contract_id'])) {
                $contractId = $this->AddYuansu(intval($param['id']), $user_info['id']);
                if (empty($contractId)) {
                    outPutData('', '合同信息错误', Config::get('code.tipsCode'));
                }
                $ocModel->where($where)->update(['contract_id' => $contractId]);
            }
            $ssqmobile = $ContInfo->ssqaccount;

            $result = $Best_data->viewContract($ContInfo['contract_id'] ? $ContInfo['contract_id'] : $contractId, $ssqmobile);

            if ($result->errno == 0) {
                outPutData(array( 'url' => $result->data->url),'获取成功');
            } else {
                outPutData('', $result->errmsg, Config::get('code.tipsCode'));
            }
        }
    }

    /**
     * 下载合同
     * @param Request $request
     * @author hey_heyang(635870453@qq.com)
     * @return string
     */
    public function DownContract(Request $request){
        if($request->isPost()) {
            $ocModel = new OrderContract();
            $user_info = $request->user_info;
            $param = $request->param();
            if (empty(intval($param['id']))) {
                outPutData('', '合同参数错误', Config::get('code.tipsCode'));
            }
            $ocinfo = $ocModel->where(['id' => intval($param['id'])])->find();
            if (empty($ocinfo)) {
                outPutData('', '合同信息错误', Config::get('code.tipsCode'));
            }
            if (empty($ocinfo['contract_id'])) {
                outPutData('', '请先查看合同后再来下载', Config::get('code.tipsCode'));
            }
            $Best_data = new BestSignDemo();
            $res = $Best_data->downloadContract($ocinfo['contract_id']);
            outPutData(array('res' =>$res),'获取成功');
        }
    }


    /**
     * 合同添加元素
     * @param $id
     * @author hey_heyang(635870453@qq.com)
     * @return string
     */
    private function AddYuansu($id,$uid){
        $cotModel = new Contract();
        $mModel = new Member();
        $Best_data = new BestSignDemo();
        $ocModel =new OrderContract();
        $where = ['id'=>$id,'user_id'=>$uid];
        $OCinfo = $ocModel->where($where)->find();
        $Cinfo = $cotModel->where(['id'=>$OCinfo['cid']])->find();
        $MemberInfo = $mModel->where(['id'=>$uid])->find();
        //姓名
        $ADDdata[] = $Best_data->writeContent($Cinfo['npage'],$Cinfo['xname'],$Cinfo['yname'],$MemberInfo['username']);
        //身份证
        $ADDdata[] = $Best_data->writeContent($Cinfo['id_card'],$Cinfo['xcard'],$Cinfo['ycard'],$MemberInfo['certificate']);
        //金额
        $ADDdata[] = $Best_data->writeContent($Cinfo['mpage'],$Cinfo['xmoney'],$Cinfo['ymoney'],$OCinfo['money']);
        //大写金额
        $ADDdata[] = $Best_data->writeContent($Cinfo['hz_page'],$Cinfo['hz_x'],$Cinfo['hz_y'],num_to_rmb($OCinfo['money']));
        //金额2
        $ADDdata[] = $Best_data->writeContent($Cinfo['mtopage'],$Cinfo['xtomoney'],$Cinfo['ytomoney'],$OCinfo['money']);
        //大写金额2
        $ADDdata[] = $Best_data->writeContent($Cinfo['hzto_page'],$Cinfo['hzto_x'],$Cinfo['hzto_y'],num_to_rmb($OCinfo['money']));
        $res = $Best_data->putContent($Cinfo['fid'],$ADDdata);
        $contractId = $Best_data->create($res->data->fid,$Cinfo['title'],$Cinfo['expiretime']);
        return $contractId;
    }
}
